<?php
session_start();
?>
<html>
    <head>
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        
    </head>
    <body>
        <form action="" method="post" onSubmit="return ValidateForm()">
            <?php
            include 'functions.php';
            $confID = intval($_GET['conference_id']);
            $crID = $_SESSION['member_id'];
            $con = mysql_connect('localhost', 'root', '');

            if (!$con) {

                ('Could not connect:' . mysql_error());
            }
            mysql_select_db("mydb", $con);

            //connects to the server and selects the database
            //Query selects the short name from the conferences table with the same conference id that was sent from
            //previous page
            $confName = get_conference_name($confID);
            print "<h1>{$confName}</h1>";

            //creates a form to create a new session and assign papers to it
            // the user enters the date in three textboxes, one for the day, one for the month, and one for the year.
            echo'<h2> Set up a Conference Session</h2>
            <br>
            
            <p>Assign papers that have been reviewed and accepted to this session: </p>';

            //query selects from the database all the papers in the conference that have a total rating (acceptance+final)
            //of more than 5, and have the session id set to null which means that they havent been assigned to a session yet
            $result = mysql_query("SELECT paper_id, paper_title FROM paper WHERE (acceptance+final_rating) >= 5 AND session_id is Null
                AND conference_id = $confID") or die(mysql_error());

            //as long as there are entries in the database, they will be retrieved in an array
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                // the checkbox list will be populated, getting as a value the paper id
                //and it will display the title of a paper
                echo "<input type=\"checkbox\" name=\"papers[]\" value=\"{$row['paper_id']}\"> {$row['paper_title']}</input><br>";
            }

            //creates a button that submits form information

            echo '</br></br><input type ="submit" name="assign" value="Create"></input></br>';

            if (isset($_POST['assign'])) {
                //an array is created to store all the values of the checkboxes that were clicked
               
                if (!isset($_POST['papers'])) {
                    echo '<script type="text/javascript">alert("Please assign at least one paper to this session");</script>';
                } else {
                    

                    $aPaper = $_POST['papers'];
                    //a variable is initialized to store the length of the array as an integer
                    $n = count($aPaper);
                    // the query inserts the session information into the database creating a new record for the session
                    mysql_query("INSERT INTO session (conference_id, creator_id)
                        VALUES ('$confID', '$crID')") or die(mysql_error());
                    // the mysql insertid method retrieves the identity value of the last record entered in the database
                    $sessID = mysql_insert_id();
                    //loops on all the values in the array
                    for ($i = 0; $i < $n; $i++) {
                        //retrieves the value in the array at position i and stores it in a variable
                        $paperid = $aPaper[$i];
                        //executes a query to update the paper record in the table with the id of the
                        //session it was assigned to
                        mysql_query("UPDATE paper SET session_id = '$sessID' WHERE paper_id = '$paperid'") or die(mysql_error());
                    }
                    
                }
            }

            //checks that the button was clicked
            //closes connection
            mysql_close($con);
            ?>


        </form>
    </body>
</html>
<head>
    <script type='text/javascript' src="sysJavascript.js"></script>
    <script type="text/javascript" src="calendar.js"></script>
</head>